<?php
/**
 * 试卷管理
 * @since   2018-06-13
 * @author  winljm001 <winljm001@126.com>
 */

namespace app\admin\controller;



use app\model\AdminTestPaper;
use app\util\ReturnCode;
use app\util\Tools;

class TestPaper extends Base {

    /**
     * 获取试卷列表
     * @return array
     * @throws \think\exception\DbException
     * @author  winljm001 <winljm001@126.com>
     */
    public function index() {

        $limit = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $keywords = $this->request->get('keywords', '');
        $type = $this->request->get('type', '');
        $status = $this->request->get('status', '');

        $where = [];
        if ($status === '1' || $status === '0') {
            $where['status'] = $status;
        }
        if ($type!='') {
            $where['type'] = $type;
        }
        
        if($keywords!=''){
            $where=array_merge($where,['id|name'=>['like', "%{$keywords}%"]]);
        }

        $listObj = (new AdminTestPaper())->where($where)->order('updateTime DESC')
            ->paginate($limit, false, ['page' => $start])->toArray();
        $listInfo = $listObj['data'];
        return $this->buildSuccess([
            'list'  => $listInfo,
            'count' => $listObj['total']
        ]);
    }

    /**
     * 新增试卷
     * @return array
     * @author  winljm001 <winljm001@126.com>
     */
    public function add() {
        $postData = $this->request->post();
        $postData['addTime'] = time();
        $postData['updateTime'] = time();
        $res = AdminTestPaper::create($postData);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
        } else {
            return $this->buildSuccess([]);
        }
    }

    /**
     * 删除试卷
     * @return array
     * @author winljm001 <winljm001@126.com>
     */
    public function del() {
        $id = $this->request->get('id');
        if (!$id) {
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
        }
        AdminTestPaper::destroy($id);
        return $this->buildSuccess([]);
    }

    /**
     * 获取试卷
     * @return object
     * @author winljm001 <winljm001@126.com>
     */
    public function getDetail() {
        $id = $this->request->get('id');
        if (!$id) {
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
        }
        $data = AdminTestPaper::get(['id' => $id]);
        return $this->buildSuccess([
            'testPaper'  => $data
        ]);
    }

    /**
     * 编辑试卷
     * @author winljm001 <winljm001@126.com>
     * @return array
     * @throws \think\exception\DbException
     */
    public function edit() {
        $postData = $this->request->post();
        $postData['updateTime'] = time();
        $res = AdminTestPaper::update($postData);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
        } else {
            return $this->buildSuccess([]);
        }
    }

    /**
     * 试卷状态编辑
     * @return array
     * @author winljm001 <winljm001@126.com>
     */
    public function changeStatus() {
        $idArr = explode(',',$this->request->get('id'));
        if ($idArr[0]== '') {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '缺少必要参数');
        }
        $status = $this->request->get('status');
        foreach ($idArr as $key => $value) {
            $res = AdminTestPaper::update([
                'Id'         => $value,
                'status'     => $status
            ]);
        }
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
        } else {
            return $this->buildSuccess([]);
        }
    }
    
}
